*Create past OP for use in checking randomisationlear

clear all

* Extract previous outpatient attendances for each patient

use "$savedata/amipats_ids0517.dta", clear
keep extract
save "$savedata/amipats_ids0517_v2.dta", replace

foreach year in 0506 0607 0708 0809 0910 1011 1112 1213 1314 1415{

use "$savedata/amipats_ids0517_v2.dta", clear

keep extract

merge 1:m extract using "$rawdata_op/op_`year'.dta"
drop if _merge==2

gen one = 0
replace one = 1 if _merge==3
gen cardio = 0
replace cardio = 1 if tretspef=="330"

gen year = apptdate_year
replace year = apptdate_year-1 if apptdate_month>0 & apptdate_month<4

egen finyear = max(year)

collapse (sum) cardio one, by(extract finyear)

gen any_visit = 0
replace any_visit = 1 if one>0 & one!=.

gen any_cardio = 0
replace any_cardio = 1 if cardio>0 & cardio!=.

rename cardio num_cardio
rename one num_visit

save "$savedata/op`year'.dta", replace
}


use "$savedata/amipats_ids0517_v2.dta", clear

keep extract

gen encrypted_hesid = extract

merge 1:m encrypted using "$rawdata_op/op_1516.dta"
drop if _merge==2

gen one = 0
replace one = 1 if _merge==3
gen cardio = 0
replace cardio = 1 if tretspef=="330"

gen finyear=2015

collapse (sum) cardio one, by(extract finyear)

gen any_visit = 0
replace any_visit = 1 if one>0 & one!=.

gen any_cardio = 0
replace any_cardio = 1 if cardio>0 & cardio!=.

rename cardio num_cardio
rename one num_visit

save "$savedata/op1516.dta", replace


use "$savedata/amipats_ids0517_v2.dta", clear

keep extract

gen encrypted_hesid = extract

merge 1:m encrypted using "$rawdata_op/op_1617.dta"
drop if _merge==2

gen one = 0
replace one = 1 if _merge==3
gen cardio = 0
replace cardio = 1 if tretspef=="330"

gen finyear=2016

collapse (sum) cardio one, by(extract finyear)

gen any_visit = 0
replace any_visit = 1 if one>0 & one!=.

gen any_cardio = 0
replace any_cardio = 1 if cardio>0 & cardio!=.

rename cardio num_cardio
rename one num_visit

save "$savedata/op1617.dta", replace


use "$savedata/amipats_ids0517_v2.dta", clear

keep extract

gen encrypted_hesid = extract

merge 1:m encrypted using "$rawdata_op/op_1718.dta"
drop if _merge==2

gen one = 0
replace one = 1 if _merge==3
gen cardio = 0
replace cardio = 1 if tretspef=="330"

gen finyear=2017

collapse (sum) cardio one, by(extract finyear)

gen any_visit = 0
replace any_visit = 1 if one>0 & one!=.

gen any_cardio = 0
replace any_cardio = 1 if cardio>0 & cardio!=.

rename cardio num_cardio
rename one num_visit

save "$savedata/op1718.dta", replace



* Then I need to merge these together and make a master dataset with all years that can be merged into the analysis data 

use "$savedata/op0506.dta", clear

foreach x in 0607 0708 0809 0910 1011 1112 1213 1314 1415 1516 1617 1718{
	append using "$savedata/op`x'.dta"
}

rename num_cardio num_cardio_op
rename num_visit num_visit_op
rename any_cardio any_cardio_op
rename any_visit any_visit_op

reshape wide num_cardio num_visit any_visit any_cardio, i(extract) j(finyear)

save "$savedata/past_op0517.dta", replace

